package com.hbnu.Dao.Impl;

import com.hbnu.Dao.DormManagerDao;
import com.hbnu.Dao.UserDao;
import com.hbnu.Pojo.DormBuild;
import com.hbnu.Util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DormManagerDaoImp implements DormManagerDao {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public List<DormBuild> findDorm() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<DormBuild> dormBuilds = new ArrayList<>();

        try {
            connection = DBUtils.getConnection();
            String sql = "select * from tb_dormbuild";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                DormBuild dormBuild = new DormBuild();
                dormBuild.setId(resultSet.getInt("id"));
                dormBuild.setName(resultSet.getString("name"));
                dormBuild.setDisabled(resultSet.getInt("disabled"));
                dormBuild.setRemark(resultSet.getString("remark"));
                dormBuilds.add(dormBuild);
            }
            return  dormBuilds;
        }catch (Exception e) {

        }finally {
            DBUtils.close(resultSet, preparedStatement, connection);
        }

        return null;
    }

    @Override
    public void savaManagerAndBuild(Integer userId, String[] dormId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtils.getConnection();
            String sql = "insert into tb_manage_dormbuild(USER_ID, Dormbuild_id) values(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            for (String dormbuildId : dormId) {
                preparedStatement.setInt(1, userId);
                preparedStatement.setString(2, dormbuildId);
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        }catch (Exception e) {

        }finally {
            DBUtils.close(null, preparedStatement, connection);
        }

    }

    @Override
    public void deleteByUserId(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtils.getConnection();
            String sql = "delete from tb_manage_dormbuild where user_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
        }catch (Exception e) {

        }finally {
            DBUtils.close(null, preparedStatement, connection);
        }
    }

    @Override
    public List<DormBuild> findByUserId(Integer id) {

        return userDao.findDormBuildsByUserId(id);
    }

    @Override
    public List<DormBuild> findAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from tb_dormbuild";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            List<DormBuild> dormBuilds = new ArrayList<>();
            while (resultSet.next()) {
                DormBuild dormBuild = new DormBuild();
                dormBuild.setId(resultSet.getInt("id"));
                dormBuild.setName(resultSet.getString("name"));
                dormBuild.setDisabled(resultSet.getInt("disabled"));
                dormBuild.setRemark(resultSet.getString("remark"));
                dormBuilds.add(dormBuild);

            }
            return dormBuilds;
        }catch (Exception e) {

        }finally {
            DBUtils.close(resultSet, preparedStatement, connection);
        }


        return null;
    }


}
